package com.wang.myblog.Interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //登录成功后应该有用户的session
        Object onlogin = request.getSession().getAttribute("admin");
        //没有登录
        if (onlogin==null){
            request.setAttribute("msg","没有获得权限，请先登录");
            request.getRequestDispatcher("/login").forward(request,response);
            return false;
        }else {
            //禁用浏览器缓存，防止退出登录后，后退操作还能看见之前的信息（虽然不能操作）
            response.setDateHeader("Expires", 0);
            response.setHeader("Cache-Control","no-store");
            response.setHeader("Pragma", "no-cache");
            return true;
        }
    }
}
